﻿using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPF.Core;
using WPF.Core.Dtos;
using WPF.Core.Services;

namespace WPF.NoteBook.ViewModels
{
    public class CreateNoteBookViewModel: HostDialogViewModel
    {
        public CreateNoteBookViewModel(INoteBookService appService)
        {
            this.appService = appService;
        }

        private NoteBookModel model;
        private readonly INoteBookService appService;

        public NoteBookModel Model
        {
            get { return model; }
            set { model = value; RaisePropertyChanged(); }
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            //编辑操作
            if (parameters.ContainsKey("Value"))
            {
                Model = parameters.GetValue<NoteBookModel>("Value");

            }
            else //新建操作
            {
                Model = new NoteBookModel();
            }
        }

        public override async void Save()
        {
            if (Model.Id == 0)
                await appService.Create(Model);
            else
                await appService.Update(Model);

            base.Save();
        }
    }
}
